﻿using Microsoft.AspNetCore.Mvc;
using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Services.Authentication;
using PMS.Services.SharedKernel;
using PMS.Services.Utils.Security;
using PMS.Web.Factories.Customers;
using PMS.Web.Framework.Controllers;
using PMS.Web.Framework.Mvc.Filters;

namespace PMS.Web.Controllers.Backend.Login
{
    public partial class BackendLoginController : BaseAdminController
    {
        #region Fields 
        private readonly IAuthenticationProvider _authProvider;
        private readonly ICustomerModelFactory _customerModelFactory;
        #endregion

        #region Ctor
        public BackendLoginController(IAuthenticationProvider authProvider,
            ICustomerModelFactory customerModelFactory)
        {
            _authProvider = authProvider;
            _customerModelFactory = customerModelFactory;
        }
        #endregion

        #region Login/Logout
        [AuthorizeCustomer(CustomerAuthorizeType.Ignore)]
        public virtual IActionResult Login(string returnUrl)
        {
            #region MovedToAuthorizeAdminAttribute
            //var curCustomer = _workContext.CurrentCustomer;
            //if (curCustomer?.IsAdminUser() ?? false)
            //    return RedirectToAction("Index", "BackendHome", new { returnUrl });
            #endregion
            if(string.IsNullOrEmpty(returnUrl))
                returnUrl = Url.RouteUrl("BackendHome");
            var model = _customerModelFactory.PrepareLoginModel(returnUrl, LoginType.Password);
            return View(model);
        }

        [AuthorizeCustomer(CustomerAuthorizeType.Ignore)]
        public virtual IActionResult Logout()
        {
            _authProvider.AuthServcice.SignOut();
            return RedirectToRoute("BackendLogin");
        }
        #endregion
    }
}
